package com.fortune.controller.app;

import com.faneui.common.bean.BaseResp;
import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.ListUtils;
import com.faneui.common.utils.MapUtils;
import com.fortune.dal.dao.*;
import com.fortune.dal.model.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("recharge")
public class RechargeAccountController {

    @Autowired
    private CustDAO custDAO;

    @Autowired
    private HttpSession httpSession;

    @Autowired
    private AccountDAO accountDAO;

    @Autowired
    private TradeQueryDAO tradeQueryDAO;

    @Autowired
    private BankDAO bankDAO;

    @Autowired
    private BankCardDAO bankCardDAO;

    @RequestMapping(value = "rechargefir", method = RequestMethod.GET)
    public ModelAndView indexOfAllOne() throws BusinessException {
        ModelAndView mv = new ModelAndView("rechargefir");
        mv.addObject("menuFlag", "rechargefir");
        return mv;
    }

    @RequestMapping(value = "rechargesec", method = RequestMethod.GET)
    public ModelAndView indexOfAllTwo() throws BusinessException {
        ModelAndView mv = new ModelAndView("rechargesec");
        mv.addObject("menuFlag", "rechargesec");
        return mv;
    }

    @RequestMapping(value = "rechargesuccess", method = RequestMethod.GET)
    public ModelAndView indexOfAllThree() throws BusinessException {
        ModelAndView mv = new ModelAndView("rechargesuccess");
        mv.addObject("menuFlag", "rechargesuccess");
        return mv;
    }

    @RequestMapping(value = "rechargechosecard", method = RequestMethod.GET)
    public ModelAndView cashInCard() throws BusinessException {
        ModelAndView mv = new ModelAndView("rechargechosecard");

        Cust cust = (Cust)httpSession.getAttribute("user");
        BankCard bankCard = new BankCard();
        bankCard.setCustId(cust.getId());
        List<Map<String,Object>> lists = new ArrayList();
        try {
            List<BankCard> list = bankCardDAO.queryByModel(bankCard);
            for(BankCard bc : list){
                Map<String,Object> map = MapUtils.parseMap(bc);
                Bank bank = bankDAO.queryById(bc.getBankId());
                String bankName = bank.getName();
                map.put("bankName",bankName);
                String imgUrl = bank.getImgUrl();
                map.put("imgUrl",imgUrl);
                String cardCode = bc.getCardCode();
                String simpleCode = cardCode.substring(cardCode.length() - 4);
                map.put("simpleCode",simpleCode);
                lists.add(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        mv.addObject("lists", lists);
        return mv;
    }

    //选卡成功
    @RequestMapping(value = "afterchosecard", method = RequestMethod.GET)
    public ModelAndView afterChoseCard(@RequestParam Integer id) throws BusinessException {
        ModelAndView mv = new ModelAndView("rechargefir");
        try {
            BankCard b = new BankCard();
            b.setId(id);
            BankCard bankCard = bankCardDAO.queryById(b.getId());
            httpSession.setAttribute("selectCard",bankCard);
            Map<String,Object> map = MapUtils.parseMap(bankCard);
            String cardCode = bankCard.getCardCode();
            String newCode = cardCode.substring(cardCode.length() - 4);
            map.put("newCode",newCode);
            Bank bank = bankDAO.queryById(bankCard.getBankId());
            String bankName = bank.getName();
            map.put("newName",bankName);
            map.put("date",new Date());

            httpSession.setAttribute("cardMap",map);

            mv.addObject("cardMap", map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }

    @RequestMapping(value = "addmoney", method = RequestMethod.POST)
    public BaseResp stepOne(@RequestBody TradeQuery tradeQuery ) throws BusinessException {
        BaseResp resp = new BaseResp();
        try {
            Double money = tradeQuery.getTradeAmount();

           if(StringUtils.isBlank(money + "")){
               throw new BusinessException("充值金额不能为空！");
           }

           httpSession.setAttribute("addAllMoney", tradeQuery.getTradeAmount());
           httpSession.setAttribute("addNewMoney", money-10);
        }catch (Exception ex){
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }


    @RequestMapping(value = "tradepass", method = RequestMethod.POST)
    public BaseResp stepTwo(@RequestBody Cust cust ) throws BusinessException {
        BaseResp resp = new BaseResp();
        try {
            if(StringUtils.isBlank(cust.getTradePassword())){
                throw new BusinessException("交易密码不能为空！");
            }
            Cust user = (Cust) httpSession.getAttribute("user");

            Cust param = new Cust();
            param.setPhone(user.getPhone());
            List<Cust> list = custDAO.queryByModel(param);
            if(ListUtils.isEmpty(list)){
                throw new BusinessException("用户未登录");
            }

            if(!(user.getTradePassword().equals(cust.getTradePassword()))){
                throw new BusinessException("交易密码输入错误");
            }

            Account account = new Account();
            account.setCustId(user.getId());

            List<Account> accountList = accountDAO.queryByModel(account);


            if(ListUtils.isEmpty(accountList)){
                throw new BusinessException("未注册账户");
            }
            Account accounts = accountList.get(0);
            Double oldAmountBalance = accounts.getAccountBalance();
            Double addmoney = (Double)httpSession.getAttribute("addNewMoney");
            Double newAmountBalance = oldAmountBalance + addmoney;


            accounts.setAccountBalance(newAmountBalance);
            accountDAO.updateById(accounts);

            //将交易记录存入数据库
            TradeQuery tradeQuery = new TradeQuery();
            tradeQuery.setTradeOperator("充值");
            tradeQuery.setTradeDate(new Date());
            Double money = (Double)httpSession.getAttribute("addAllMoney");
            tradeQuery.setTradeAmount(money);
            tradeQuery.setTradeStatus("交易成功");
            tradeQuery.setCustId(user.getId());
            BankCard bankCard = (BankCard) httpSession.getAttribute("selectCard");
            tradeQuery.setBankCardId(bankCard.getId());

            tradeQueryDAO.insert(tradeQuery);

        }catch (Exception ex){
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }
}
